package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResBanner;
import com.ruoyi.resturant.service.IResBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLDataException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@RestController
@RequestMapping("/api/resturant/banner")
@Api(tags = "轮播图管理")
public class ResBannerController {

    @Autowired
    private IResBannerService resBannerService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 取得所有轮播图列表
     * @param bannerName
     * @return
     */
    @ApiOperation("取得所有轮播图列表")
    @ApiImplicitParam(name = "name", value = "轮播图名称",
            dataType = "string", dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name",required = false) String bannerName){
        List<ResBanner> list = resBannerService.selectResBannerByParam(bannerName);
        return AjaxResult.success(list);
    }

    /**
     * 获取轮播图详细信息
     */
    @ApiOperation(value = "获取轮播图详细信息", notes = "TODO：取得某个轮播图的详细信息")
    @ApiImplicitParam(name = "id", value = "轮播图ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        // 1从数据库里查询
        ResBanner resBanner = resBannerService.selectResBannerById(id);
        // 2 数据库里该轮播图ID不存在的场合，抛出业务异常BizException
        if(resBanner == null) {
            throw new BizException(404, "该轮播图id("+ id + ")不存在");
        }
        String strDB = JSON.toJSONString(resBanner);
        // 3 存储到Redis数据库中
        redisCache.setCacheObject(String.valueOf(id), strDB,
                1, TimeUnit.HOURS);
        // 4 返回取得model对象
        return AjaxResult.success(resBanner);
    }



    /**
     * 新增轮播图
     */
    @Log(title = "轮播图管理",businessType = BusinessType.INSERT)
    @ApiOperation("新增轮播图")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ResBanner resBanner) {
        return AjaxResult.success(resBannerService.insertResBanner(resBanner));
    }

    /**
     * 修改轮播图
     */
    @Log(title = "轮播图管理",businessType = BusinessType.UPDATE)
    @ApiOperation("修改轮播图")
    @PutMapping
    public AjaxResult edit( @RequestBody ResBanner resBanner) {
        //1 先根据轮播图ID查询数据库，取得该轮播图对象
        ResBanner resBannerDB=resBannerService.selectResBannerById(resBanner.getId());
        //2 如果该轮播图对象不存在，就抛出业务异常
        if(resBannerDB == null){
            throw new BizException(404,"该轮播图id("+resBanner.getId() + ")不存在");
        }
        //3 如果该轮播图存在，执行更新操作
        return AjaxResult.success(resBannerService.updateResBanner(resBanner));
    }

    /**
     * 删除轮播图
     */
    @Log(title = "轮播图管理",businessType = BusinessType.DELETE)
    @ApiOperation("删除轮播图")
    @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long",
            paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(resBannerService.deleteResBannerById(id));
    }

}